<template>
	<view class="container">
		<view class="toolbar" @tap="format">
			<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"></view>
			<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align" data-value="left"></view>
			<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi" data-name="align" data-value="center"></view>
			<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align" data-value="right"></view>
			<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi" data-name="align" data-value="justify"></view>
			<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight" data-value="2"></view>
			<view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing" data-name="letterSpacing" data-value="2em"></view>
			<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-duanqianju" data-name="marginTop" data-value="20px"></view>
			<view :class="formats.marginBottom ? 'ql-active' : ''" class="iconfont icon-duanhouju" data-name="marginBottom" data-value="20px"></view>
			<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
			<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-font-size" data-name="fontSize" data-value="24px"></view>
			<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie" data-name="list" data-value="ordered"></view>
			<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list" data-value="bullet"></view>
			<view class="iconfont icon-undo" @tap="undo"></view>
			<view class="iconfont icon-redo" @tap="redo"></view>
			<view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
			<view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
			<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
			<view class="iconfont icon-charutupian" @tap="insertImage"></view>
			<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-formatheader1" data-name="header" :data-value="1"></view>
			<view class="iconfont icon-shanchu" @tap="clear"></view>
		</view>

		<view class="editor-wrapper">
			<editor id="editor" class="ql-container" :placeholder="placeholder" show-img-size show-img-toolbar show-img-resize
				:read-only="readOnly" @ready="onEditorReady" @statuschange="onStatusChange" @input="inputOver">
			</editor>
		</view>
	</view>
</template>

<script>
	export default {
		props: ['content', 'placeholder', 'index'],
		data() {
			return {
				readOnly: false,
				formats: {} // 已设置的样式
			}
		},
		methods: {
			onEditorReady() {
                const query = uni.createSelectorQuery().in(this)
                query.select('#editor').context((res) => {
					this.editorCtx = res.context

					// 初始化编辑器内容
					this.editorCtx.setContents({
						html: this.content
					})
				}).exec()
			},
			undo() { // 撤销
				this.editorCtx.undo()
			},
			redo() { // 恢复
				this.editorCtx.redo()
			},
			format(e) {
				let { name, value } = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.editorCtx.format(name, value) // 修改样式
			},
			onStatusChange(e) {
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
			},
			clear() {
				uni.showModal({
					title: '清空编辑器',
					content: '确定清空编辑器全部内容？',
					success: res => {
						if (res.confirm) {
							this.editorCtx.clear({
								success: function(res) {
									console.log("clear success")
								}
							})
						}
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						let path = res.tempFilePaths[0]
						this.$common.uploadFiles(path).then(result => {
							if(result.errcode != 100) return this.$Toast('上传失败')
							let Url = result.url.slice(22)
							this.editorCtx.insertImage({
								src: this.$imgUrls(Url),
								alt: '图像',
								success: function() {
									console.log('insert image success')
								}
							})
						})
					}
				})
			},
			inputOver(e) { // index：表单项下标
				this.$emit('getContents', e.detail.html, this.index)
			}
		}
	}
</script>

<style>
	@import "./editor-icon.css";

	.editor-wrapper {
		height: 500rpx;
		background: #fff;
	}

	.iconfont {
		display: inline-block;
		margin: 8px 8px 0 0;
		font-size: 20px;
	}

	.ql-container {
		box-sizing: border-box;
		padding-top: 12px;
		width: 100%;
		height: 100%;
		font-size: 14px;
		line-height: 1.5;
	}

	.ql-active {
		color: #06c;
	}
</style>
